package com.qkst.website.backstage.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.qkst.website.backstage.bean.InventoryBean;
import com.qkst.website.backstage.bean.OfferPriceBean;
import com.qkst.website.backstage.bean.ProductBean;
import com.qkst.website.backstage.mapper.InventoryMapper;
import com.qkst.website.backstage.mapper.OfferPriceMapper;
import com.qkst.website.backstage.mapper.ProductMapper;
import com.qkst.website.backstage.mapper.ProductRelAttrMapper;
import com.qkst.website.backstage.service.ProductService;
import com.qkst.website.common.utils.SetEnums;

/**
 * @author lh
 * @description 产品service 业务处理层
 * @date 2017-05-03
 * @email ahlxlihao@163.com
 *
 */
@Service("productService")
@Transactional
public class ProductServiceImpl implements ProductService {
	@Resource(name = "productMapper")
	private ProductMapper productMapper;//产品
	@Autowired
	private OfferPriceMapper offerPriceMapper;//产品价格
	@Autowired
	private InventoryMapper inventoryMapper;//产品库存
	@Autowired
	private ProductRelAttrMapper productRelAttrMapper;//产品属性

	/**
	 * 获取产品
	 * @param product
	 * @return List<ProductBean>
	 */
	
	@Override
	public List<ProductBean> list(ProductBean product) {
		return productMapper.selectProducts(product);
	}
	/**
	 * 保存产品
	 * @param product
	 * @return ProductBean
	 */
	@Override
	public ProductBean save(ProductBean product) {
		product.setCreateTime(new Date());
		product.setLastUpdate(new Date());
		//插入产品基本信息
		productMapper.insert(product);
		//插入属性 
		if(product.getAttrList()!=null && !product.getAttrList().isEmpty()){
			productRelAttrMapper.bacthInsert(product);
		}
		//如果是PRODUCT
		if(SetEnums._PRODUCT_BEAN.toString().equals(product.getProductType())){
			//插入价格
			OfferPriceBean offerPrice=product.getOfferPrice();
			if(offerPrice!=null){
				offerPrice.setProductId(product.getProductId());
				offerPriceMapper.insert(offerPrice);
			}
		}else if(SetEnums._SKU_BEAN.toString().equals(product.getProductType())){//如果是SKU
			//插入库存
			InventoryBean inventory=product.getInventory();
			if(inventory!=null){
				inventory.setProductId(product.getProductId());
				inventoryMapper.insert(inventory);
			}
		}
		return product;
	}
	
	/**
	 * 删除产品
	 * @param product
	 * @return boolean
	 */
	@Override
	public boolean delete(ProductBean product) {
		int num=productMapper.deleteByPrimaryKey(product.getCategoryId());
		if(num>0){
			return true;
		}
		return false;
	}

}
